package com.gxa.park.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxa.park.core.commom.ExceptionCode;
import com.gxa.park.core.commom.JsonResult;
import com.gxa.park.core.exception.BusinessException;
import com.gxa.park.core.pojo.domain.Inspector;
import com.gxa.park.core.util.JWTUtil;
import com.gxa.park.core.util.StringUtil;
import com.gxa.park.manager.jwt.NeedsLogin;
import com.gxa.park.manager.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.util.Map;

/**
 * @date 2022/7/19
 */
@RestController
@RequestMapping("/inspector")
@Api("巡检员的api")
public class InspectorController {

    LambdaQueryWrapper<Inspector> lambda=null;

    @Autowired
    ManagerService managerService;

    @ApiOperation(value = "登录的接口",produces = "query")
    @PostMapping("/login")
    public Map login(@NotEmpty(message = "手机号不能为空")
//                         @Length(min = 11,max = 11,message = "输入11位手机号")
//                         @Pattern(regexp = "^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0135678])|(18[0-9])|(19[89]))\\d{8})$",
//                                 message = "手机号格式错误")
                         @RequestParam(value = "手机号")String phone,
                     @NotEmpty(message = "密码不能为空")
//                     @Length(min = 6,max = 6,message = "密码长度不正确")
                     @RequestParam("密码")String pwd) throws Exception {

        if(StringUtil.isEmpty(pwd) || StringUtil.isEmpty(phone) || !StringUtil.isPhoneNumber(phone)){
            throw new BusinessException(ExceptionCode.PHONE_PWD_ISEMTY);
        }
        lambda = Wrappers.lambdaQuery(Inspector.class);
        lambda.eq(Inspector::getInPhone,phone);
        lambda.eq(Inspector::getInPwd,pwd);
        Inspector login = managerService.getOne(lambda);

// 登录成功, 生成jwt返回给前端
        String token = JWTUtil.sign(phone);


        if(login == null){
            throw new BusinessException(ExceptionCode.ADMIN_NOT_EXIST);
        }
        JsonResult jsonResult =new JsonResult();
        jsonResult.put("token",token);
        jsonResult.put("name",login.getInName());
       jsonResult.setCode("1");
       jsonResult.setMsg("登录成功");

        return jsonResult.getMap();
    }

//    @PostMapping("/selectById")
//    @NeedsLogin
//    public Map selectById(Long id){
//        Inspector byId = managerService.getById(id);
//
//        JsonResult jsonResult= new JsonResult();
//        jsonResult.setCode("1");
//        jsonResult.setMsg("查询成功");
//        jsonResult.setData(byId);
//        return jsonResult.getMap();
//    }


}
